// pages/cost/result/index.js
const app = getApp();
import api from '../../../../services/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonBounding: app.globalData.buttonBounding,
    type: '',
    qd: '',
    qdName: '',
    productList: [],
    queryProductIds: '',
    resultInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    if (e.type) {
      let qdName= '零售';
      if (e.qd === 'ds') {
        qdName = '电商'
      } else if (e.qd === 'pdi') {
        qdName = 'PDI'
      }
      let productListObj = JSON.parse(e.productList);
      let queryProductIds = '';
      for (let i = 0; i < productListObj.length; i ++) {
        if (queryProductIds) {
          queryProductIds = queryProductIds + ',' + productListObj[i].productId;
        } else {
          queryProductIds = productListObj[i].productId;
        }
      }
      this.setData({
        type: e.type,
        qd: e.qd,
        qdName: qdName,
        productList: productListObj,
        queryProductIds: queryProductIds
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryPriceInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goBack() {
    wx.navigateBack();
  },
  async queryPriceInfo() {
    //seriesName productId productName
    const { queryProductIds, productList } = this.data;
    let params = {
      productIdList: queryProductIds
    };
    const res = await api.quote.queryWindowPrice(params);
    if (res.status == 200) {
      for (let i = 0; i < res.data.priceList.length; i ++) {
        let introduceObj = JSON.parse(res.data.priceList[i].introduce);
        let introduceObjLeft = [];
        let introduceObjRight = [];
        for (let j = 0; j < introduceObj.length; j ++) {
          if (j % 2 === 0) {
            introduceObjLeft.push(introduceObj[j]);
          } else {
            introduceObjRight.push(introduceObj[j]);
          }
        }
        res.data.priceList[i].introduceObjLeft = introduceObjLeft;
        res.data.priceList[i].introduceObjRight = introduceObjRight;
        for (let j = 0; j < productList.length; j ++) {
          if (res.data.priceList[i].productId === productList[j].productId) {
            res.data.priceList[i].seriesName = productList[j].seriesName
          }
        }
      }
    }
    this.setData({
      resultInfo: res.data
    })
  },
})